package com.pctx.bibawxminiprogram.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pctx.bibacommon.util.DateTimeUtil;
import com.pctx.bibawxminiprogram.dao.UserMapper;
import com.pctx.bibawxminiprogram.pojo.User;
import com.pctx.bibawxminiprogram.pojo.UserExample;
import com.pctx.bibawxminiprogram.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 关于用户的业务层的处理
 * Created by wnf on 2018/3/10.
 */
@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过openId去数据库中查询出其用户信息
     * @param openId
     * @return
     * @throws Exception
     */
    @Override
    public User getUserByOpenId(String openId) throws Exception {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andOpenidEqualTo(openId);
        // 通过openId来查询出用户的信息
        List<User> users = userMapper.selectByExample(userExample);

        if(users != null && !users.isEmpty()){
            return users.get(0);
        }
        return null;
    }

    /**
     * 通过用户的UUID来查询出其用户信息并返回
     * @param uuid 某一个用户的UUID
     * @return 指定UUID的用户信息
     * @throws Exception
     */
    @Override
    public User getUserByUUid(String uuid) throws Exception {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUuidEqualTo(uuid);
        List<User> users = userMapper.selectByExample(userExample);
        if(users != null && !users.isEmpty()){
            return users.get(0);
        }
        return null;
    }

    /**
     * 更新用户信息
     * @param userUuid
     * @param userInfo
     * @return
     * @throws Exception
     */
    @Override
    public int updateUserInfo(String userUuid, JSONObject userInfo) throws Exception {
        // 先通过用户的UUID去查询用户对象
        User userByUUid = getUserByUUid(userUuid);

        if(userInfo != null){
            if (userInfo.containsKey("openid")){
                userByUUid.setOpenid(userInfo.getString("openid"));
            }
            // 如果是获取userinfo，那必定会有nickname属性，如果这个没有其他就没有必要一个一个判断了
            if (userInfo.containsKey("nickname")) {
                userByUUid.setNickName(userInfo.getString("nickname"));
                try {
                    if (userInfo.containsKey("headimgurl")){
                        userByUUid.setHeadImgUrl(URLDecoder.decode(userInfo.getString("headimgurl"), "utf-8"));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (userInfo.containsKey("city")){
                    userByUUid.setCity(userInfo.getString("city"));
                }
                if (userInfo.containsKey("country")){
                    userByUUid.setCountry(userInfo.getString("country"));
                }
                if (userInfo.containsKey("province")){
                    userByUUid.setProvince(userInfo.getString("province"));
                }
                if (userInfo.containsKey("gender")){
                    userByUUid.setSex(userInfo.getInteger("gender"));
                }
                userByUUid.setUpdateTime(DateTimeUtil.nowTimestamp());
                UserExample userExample = new UserExample();
                userExample.createCriteria().andUuidEqualTo(userUuid);
                // 有选择的更新用户的一些信息
                return userMapper.updateByExampleSelective(userByUUid,userExample);
            }

        }
        return 0;
    }

    /**
     * 绑定手机号到指定的用户
     * @param userUuid
     * @param Mobile
     * @return
     * @throws Exception
     */
    @Override
    public int bindMobileToUser(String userUuid, String Mobile) throws Exception {
        // 先通过用户的uuid去查询出这个用户
        User user = getUserByUUid(userUuid);
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUuidEqualTo(userUuid);
        user.setMobile(Mobile);
        user.setUpdateTime(DateTimeUtil.nowTimestamp());
        return userMapper.updateByExampleSelective(user,userExample);
    }
}
